package ext.tianma.doc.util;

import ext.com.workflow.WorkflowUtil;
import ext.tianma.bom.util.GenericUtil;
import wt.doc.WTDocument;
import wt.doc.WTDocumentDependencyLink;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.fc.collections.WTArrayList;

/**
 * @Description ProcessDocumentWorkflowUtil
 * 将工艺文件和工艺类原档文档最终状态设为一致
 * @Author MaggieHao
 * @Date 2019/7/24 10:20
 * @Version 1.0
 **/
public class ProcessDocumentWorkflowUtil {
    // 工艺类原档文档图纸
    private static final String PROCESS_DRAWING_DOC = "ProcessOriginDrawingDoc";

    //将工艺文件以及工艺文件的原档文档都设为已发布状态
    public static WTArrayList setRelease(Object object, String state) throws Exception {
        System.out.println("maggie-beginning~");

        WTArrayList wtList = new WTArrayList();
        //如果是工艺文档查询其相应的关联
        if ((object instanceof WTDocument)) {
            WTDocument doc = (WTDocument) object;
            System.out.println("maggie-获取到的对象:" + ((WTDocument) object).getNumber());
            QueryResult queryResult = PersistenceHelper.manager.navigate(doc, "describedBy", WTDocumentDependencyLink.class, false);

            System.out.println("获取相关文档queryResult：" + queryResult);
            if (queryResult != null) {
                System.out.println("获取相关文档queryResult size：" + queryResult.size());
                while (queryResult.hasMoreElements()) {
                    WTDocumentDependencyLink link = (WTDocumentDependencyLink) queryResult.nextElement();
                    if (link == null) return null;

                    WTDocument relateDoc = link.getDependsOn();
                    if (relateDoc != null) {
                        System.out.println("relateDoc1:" + relateDoc.getName() + ":" + relateDoc.getNumber());

                        String relateSoftType = GenericUtil.getTypeId(relateDoc);//判断是否是工艺类文档类型
                        if (relateSoftType.contains(PROCESS_DRAWING_DOC) && relateDoc.isLatestIteration()) {
                            System.out.println("maggie-开始设置状态-state:" + state);
                            WorkflowUtil.setLifeCycleState(relateDoc, state);
                            System.out.println("maggie-设置状态结束");
                            wtList.add(relateDoc);
                        }
                    }
                }
            }
        }
        System.out.println("maggie-ending~");
        return wtList;

    }
}